package com.Smart.smartscals.DataBase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.Smart.smartscals.Entity.DownLoadDataEntity;
import com.Smart.smartscals.Entity.EntityConversionTime;
import com.Smart.smartscals.Entity.SaveBodyInfoEntity;
import com.Smart.smartscals.Utils.MSConversionToValues;



import android.content.Context;
import android.database.Cursor;
/**
 * 此类用于处理下载用户的数据的相关方法
 * 
 */
public class DownLoadDataUtils {
	private DataBaseService mDataService;
	private Context context;

	public DownLoadDataUtils(Context context){
		this.context = context;
		mDataService = DataBaseService.getInstance(context);
	}

	/**
	 * 保存下载时候的时间
	 * @param mUserName
	 * @param mloadTime
	 */
	public void insertloadTimeInfo(String mUserName,String mloadTime){

		mDataService.insertLoadTimeInfo(mUserName, mloadTime);

	};
	/**
	 * 查找最后一次下载的时间是什么时候，
	 * 如果返回为空，说明是以前没有下载过数据就使用默认1970-01-01 12:00:00
	 * 如果不为空就使用查询出来的时间
	 * @param muserName
	 * @return
	 */
	public String getLastloadTime(String muserName){
		String lastLoadTime = null ;
		Cursor cursor = mDataService.getLastLoadTime(muserName);
		if(cursor.moveToLast()){
			lastLoadTime = cursor.getString(cursor.getColumnIndex("loadTime"));

		}
		if(cursor!=null){
			cursor.close();
		}
		return lastLoadTime ;
	}
	/**
	 * 此方法用于解析下载出来的数据，并保存到一个list集合中。
	 * @param jason
	 * @return
	 */
	public List<DownLoadDataEntity> parserDownloadData(String jsonStr){

		List<DownLoadDataEntity> list = new ArrayList<DownLoadDataEntity>();
		try {
			JSONObject jsonObject = new JSONObject(jsonStr);
			JSONArray jsArray = jsonObject.getJSONArray("t_BodyCompositionData");
			for (int i = 0; i < jsArray.length(); i++) {
				JSONObject downLoadObject =jsArray.getJSONObject(i);
				DownLoadDataEntity downLoadDataEntity = new DownLoadDataEntity();
				downLoadDataEntity.setData(downLoadObject.getString("date"));
				downLoadDataEntity.setFat(downLoadObject.getString("visceralfat"));
				downLoadDataEntity.setWeight(downLoadObject.getString("weight"));
				list.add(downLoadDataEntity);
			}
			return list;

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}
	//这个等我明天回来再写2014-12-25
	/**
	 * 先通过mdownloadTime去本地查询有没有数据
	 * 有则不插入
	 * 没有就要插入到数据库中
	 * @param bodyInfoEntity
	 * @param mdownloadTime
	 */
	public void insertLoadDownData(SaveBodyInfoEntity bodyInfoEntity,long mdownloadTime){
		long endTime = mdownloadTime + 24*3600*1000;
		String userName = bodyInfoEntity.getUserName();
		float fat = bodyInfoEntity.getBodyFat();
		float weight = bodyInfoEntity.getWeight();

		Cursor cursor = mDataService.seletBodyInfo(mdownloadTime, endTime, userName);
		MSConversionToValues msConversionToValues = new MSConversionToValues();

		if(cursor.moveToNext()){
			//进来，说明是数据库中有这个数据,取平均值再插入
			long mdate = cursor.getLong(cursor.getColumnIndex("date"));
			float bodyfat = (fat + cursor.getFloat(cursor.getColumnIndex("bodyFat"))) / 2;
			float mweight = (weight+cursor.getFloat(cursor.getColumnIndex("weight")))/2;

			//把毫秒值转化为年份、月份、星期数、日期数
			EntityConversionTime timeEntity = msConversionToValues.mSConverTOValues(mdate);
			//插入数据库，数据
			mDataService.updateBodyInfo(mdate, mweight, bodyfat, timeEntity.getWeekNUM(), timeEntity.getMonthNUM(), timeEntity.getDayNUM(), timeEntity.getYearNUM(), userName);

		}else{

			//说明数据库中没有这个日期的数据。直接插入数据就可以了。
			long mdate = bodyInfoEntity.getDate();
			//把毫秒值转化为年份、月份、星期数、日期数
			EntityConversionTime timeEntity = msConversionToValues.mSConverTOValues(mdate);
			mDataService.insertBodyInfo(mdate, weight, fat,
			timeEntity.getWeekNUM(), timeEntity.getMonthNUM(), timeEntity.getDayNUM(), timeEntity.getYearNUM(), userName);
		}
	}
}
